package cn.xspace.wnx.db;

import java.util.ArrayList;

import cn.xspace.wnx.entity.NewFriendBean;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by liuchen on 2016/10/26.
 */

public class MyNewFriendDao {

    private static MyNewFriendDao instance;
    public static final String TABLE_NAME = "t_new_friend_list";

    public static MyNewFriendDao getInstance() {
        if (instance == null) {
            instance = new MyNewFriendDao();
        }
        return instance;
    }

    public static final String COLUMN_INSERT_TIME = "insert_time";
    public static final String COLUMN_ADD_STATUS = "add_status";
    public static final String COLUMN_FRIEND_ID = "friend_id";
    public static final String COLUMN_FRIEND_NAME = "friend_name";
    public static final String COLUMN_FRIEND_LOGO = "friend_logo";
    public static final String COLUMN_FRIEND_SEX = "friend_sex";
    public static final String COLUMN_USER_ID = "user_id";
    public static final String COLUMN_USER_LOGO = "user_logo";
    public static final String COLUMN_USER_NAME = "user_name";
    public static final String COLUMN_REQ_DESC = "req_desc";
    public static final String COLUMN_IS_ADD = "unread_status";
    public static final String COLUMN_UNREAD_COUNT = "unread_count";
    public static final String COLUMN_REMARK1 = "remark1";
    public static final String COLUMN_REMARK2 = "remark2";
    public static final String COLUMN_REMARK3 = "remark3";

    public static final String NEW_FRIEND_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS "
            + MyNewFriendDao.TABLE_NAME
            + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT,"
            + MyNewFriendDao.COLUMN_UNREAD_COUNT + " INTEGER,"
            + MyNewFriendDao.COLUMN_ADD_STATUS + " INTEGER,"
            + MyNewFriendDao.COLUMN_INSERT_TIME + " INTEGER,"
            + MyNewFriendDao.COLUMN_FRIEND_ID + " TEXT,"
            + MyNewFriendDao.COLUMN_FRIEND_NAME + " TEXT,"
            + MyNewFriendDao.COLUMN_FRIEND_LOGO + " TEXT,"
            + MyNewFriendDao.COLUMN_FRIEND_SEX + " TEXT,"
            + MyNewFriendDao.COLUMN_USER_ID + " TEXT,"
            + MyNewFriendDao.COLUMN_USER_LOGO + " TEXT,"
            + MyNewFriendDao.COLUMN_USER_NAME + " TEXT,"
            + MyNewFriendDao.COLUMN_IS_ADD + " TEXT,"
            + MyNewFriendDao.COLUMN_REQ_DESC + " TEXT,"
            + MyNewFriendDao.COLUMN_REMARK1 + " TEXT,"
            + MyNewFriendDao.COLUMN_REMARK2 + " TEXT,"
            + MyNewFriendDao.COLUMN_REMARK3 + " TEXT)";

    public Observable<Long> insertOrUpdate(final NewFriendBean bean) {
        return Observable.create(new Observable.OnSubscribe<Long>() {
            @Override
            public void call(Subscriber<? super Long> subscriber) {
                long result = WNXDBManager.getInstance().insertNewFriend(bean);
                subscriber.onNext(result);
                subscriber.onCompleted();
            }
        });
    }

//    public void insertOrUpdateUnReadCount() {
//        WNXDBManager.getInstance().insertOrUpdateUnReadCount();
//    }

    public Observable<ArrayList<NewFriendBean>> getNewFriendList() {
        return Observable.create(new Observable.OnSubscribe<ArrayList<NewFriendBean>>() {
            @Override
            public void call(Subscriber<? super ArrayList<NewFriendBean>> subscriber) {
                ArrayList<NewFriendBean> beanList = WNXDBManager.getInstance().getNewFriendList();
                subscriber.onNext(beanList);
                subscriber.onCompleted();
            }
        });
    }

    public void clearUnReadCount() {
        WNXDBManager.getInstance().clearUnReadCount();
    }

    public int getUnReadCount() {
        return WNXDBManager.getInstance().getUnReadCount();
    }

    /**
     * 删除某条好友申请
     */
    public Observable<Integer> deleteOneFriendApplication(final NewFriendBean bean) {
        return Observable.create(new Observable.OnSubscribe<Integer>() {
            @Override
            public void call(Subscriber<? super Integer> subscriber) {
                subscriber.onNext(WNXDBManager.getInstance().deleteOneFriendApplication(bean));
                subscriber.onCompleted();
            }
        });
    }

    public void updateFriendStatus(final NewFriendBean bean) {
        WNXDBManager.getInstance().updateFriendStatus(bean);
//        return Observable.create(new Observable.OnSubscribe<Long>() {
//            @Override
//            public void call(Subscriber<? super Long> subscriber) {
//                long result = WNXDBManager.getInstance().updateFriendStatus(bean);
//                subscriber.onNext(result);
//                subscriber.onCompleted();
//            }
//        });
    }
}
